﻿Imports PhDir.Repository
Imports PhDirForm.Util
Imports PhDir.Model
Imports System.ComponentModel

Public Class EditName

    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        Me.UserRepository = PhDir.Main.Instance.UserRepository
    End Sub

    Private Property UserRepository As UserRepository

    Public Overloads Sub Show(ByVal userId As String)
        Me.Visible = True
        If Not String.IsNullOrEmpty(userId) Then
            Me.UserIdTextBox.Text = userId
            Me.UserIdTextBox.Enabled = False
        End If
    End Sub

    Private Sub EditName_VisibleChanged(sender As Object, e As EventArgs) Handles MyBase.VisibleChanged
        Me.UserIdTextBox.Enabled = True
        Me.ResetButton_Click(Me, EventArgs.Empty)
    End Sub

    Private Sub ResetButton_Click(sender As Object, e As EventArgs) Handles ResetButton.Click
        If Me.UserIdTextBox.Enabled = True Then
            Me.UserIdTextBox.Text = String.Empty
        End If
        Me.NameTextBox.Text = String.Empty
    End Sub

    Private Sub SaveButton_Click(sender As System.Object, e As System.EventArgs) Handles SaveButton.Click
        Dim user = New User With
                   {
                       .UserId = Me.UserIdTextBox.Text,
                       .UserName = Me.NameTextBox.Text
                   }
        Dim isSuccess = Me.UserRepository.Update(user, user)
        If isSuccess Then
            MessageBox.Show("Operation Succeed")
        Else
            MessageBox.Show("Operation Failed")
        End If
    End Sub
End Class
